package com.sl.crawler.robot.parse.impl;

import com.gargoylesoftware.htmlunit.html.DomAttr;
import com.gargoylesoftware.htmlunit.html.DomNode;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.sl.crawler.robot.parse.IParse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shiling on 2017/5/26.
 */
public class HtmlUnitParse implements IParse<List<List<String>>, HtmlPage> {
    private final static Logger LOG = LoggerFactory.getLogger(HtmlUnitParse.class);

    public List<Map<String, String>> parse(List<List<String>> xpathConfig, HtmlPage page) {
        List<Map<String, String>> resultList = new ArrayList<>();
        // 获取循环元素个数
        List<HtmlElement> htmlElementList = page.getByXPath(xpathConfig.get(0).get(2));
        for (HtmlElement htmlElement : htmlElementList) {
            Map<String, String> resultMap = new LinkedHashMap<>();
            for (List<String> config : xpathConfig) {
                String xpath = config.get(3);
                String fieldName = config.get(0);
                Object o = htmlElement.getFirstByXPath(xpath);
                if (o == null) {
                    continue;
                }
                DomNode domNode = (DomNode) o;
                String fieldValue = domNode.asText();
                if (o instanceof DomAttr) {
                    fieldValue = domNode.getNodeValue();
                }
                resultMap.put(fieldName, fieldValue);
            }
            resultList.add(resultMap);
        }

        return resultList;
    }

}
